import {
  GetPickupAddressPageData,
  AddPickupAddress,
  EditPickupAddress,
  DeletePickupAddress,
  SetPickupAddressEnable,
  OpenSelfFetchDelivery,
  CloseSelfFetchDelivery,
  GetSelfFetchConfig,
  SaveSelfFetchSupportPay
} from '@/services/order/selfFetch';
import {catchException} from '@/utils/utils';
export default {
  namespace: 'selfFetch',

  state: {
    addressList:[],
    selfFetchConfig:{}
  },

  effects: {

      // 获取上门自提地址列表
      *GetPickupAddressPageData({payload, callBack}, {call, put}) {
          const data = yield call(GetPickupAddressPageData, payload);
          yield catchException(function* (){
            if(data&&data.Code === 0) {
                yield put({
                    type: 'saveGetPickupAddressPageData',
                    payload: {
                        dataList: data.Data,
                    },
                });
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data);
            }
        })            
      },

      // 获取上门自提配置信息
      *GetSelfFetchConfig({payload, callBack}, {call, put}) {
        const data = yield call(GetSelfFetchConfig);
        yield catchException(function* (){
            if(data&&data.Code === 0) {
                yield put({
                    type: 'saveGetSelfFetchConfig',
                    payload: {
                        Data: data.Data,
                    },
                });
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data);
            }
        })          
      },

      // 增加自提地址信息
      *AddPickupAddress({payload, callBack}, {call, put}) {
        const data = yield call(AddPickupAddress, payload);
        yield catchException(function* (){
            if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
        })          
      },

      // 编辑自提地址信息
      *EditPickupAddress({payload, callBack}, {call, put}) {
        const data = yield call(EditPickupAddress, payload);
        yield catchException(function* (){
            if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
        })         
      },

      // 删除自提地址信息
      *DeletePickupAddress({payload, callBack}, {call, put}) {
        const data = yield call(DeletePickupAddress, payload);
        yield catchException(function* (){
            if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
        })          
      },

      // 启用/禁用自提点
      *SetPickupAddressEnable({payload, callBack}, {call, put}) {
        const data = yield call(SetPickupAddressEnable, payload);
        yield catchException(function* (){
            if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
        })          
      },

      // 开启上门自提
      *OpenSelfFetchDelivery({payload, callBack}, {call, put}) {
        const data = yield call(OpenSelfFetchDelivery, payload);
        yield catchException(function* (){
            if(data&&data.Code === 0) {
                callBack && callBack.success&&callBack.success(data);
            } else {
                callBack && callBack.error&&callBack.error(data)
            }
        })         
      },
      // 关闭上门自提
      *CloseSelfFetchDelivery({payload, callBack}, {call, put}) {
        const data = yield call(CloseSelfFetchDelivery, payload);
        yield catchException(function* (){
            if(data&&data.Code === 0) {
                callBack && callBack.success&&callBack.success(data);
            } else {
                callBack && callBack.error&&callBack.error(data)
            }
        })          
      },

      // 上门自提支付方式保存
      *SaveSelfFetchSupportPay({payload, callBack}, {call, put}) {
        const data = yield call(SaveSelfFetchSupportPay, payload);
        yield catchException(function* (){
            if(data&&data.Code === 0) {
                callBack && callBack.success&&callBack.success(data);
            } else {
                callBack && callBack.error&&callBack.error(data)
            }
        })          
      },


  },

  reducers: {
    saveGetPickupAddressPageData(state, {payload}) {
        return {
            ...state,
            addressList: payload.dataList,
        };
    },
    saveGetSelfFetchConfig(state, {payload}) {
      return {
          ...state,
          selfFetchConfig: payload.Data,
      };
    },
  }
}
